<?php
/*
 * $Author ：PHPYUN开发团队
 *
 * 官网: http://www.phpyun.com
 *
 * 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
 *
 * 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */

class crm_concern_controller extends adminCommon
{

    function index_action()
    {
        $crmM               =   $this -> MODEL('crm');
        $adminM             =   $this -> MODEL('admin');
        $where              =   array();
        $where['auid']      =   $_SESSION['auid'];
		$time				=	intval($_GET['day']);
		
		if(!empty($time)){

			if($time == 1){ //今天
			    
			    $startTime   =   mktime(0,0,0,date('m'),date('d'),date('y'));
			    $endTime     =   time();  

			}else if($time == 2){//昨天
			    
			    $startTime   =   mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 86400;
			    $endTime     =   mktime(23, 59, 59, date('m'), date('d'), date('Y')) - 86400;
			
			}else if($time == 3){//本周
			    
			    $startTime   =	strtotime(date('Y-m-d', strtotime("this week Monday", time())));
			    $endTime     =	strtotime(date('Y-m-d', strtotime("this week Sunday", time()))) + 24 * 3600 - 1;
			
			}else if($time == 4){//本月
			    
			    $startTime   =	mktime(0, 0, 0, date('m'), 1, date('Y'));
			    $endTime     =	mktime(23, 59, 59, date('m'), date('t'), date('Y'));

			}
			
			$where['PHPYUNBTWSTART_A']   =   '';
			
			$where['time'][]             =   array('>=', $startTime, 'AND');
			$where['time'][]             =   array('<=', $endTime,'AND');
			
			$where['PHPYUNBTWEND_A']     =   '';
			
			
			$urlarr['day']  =   $time;
		}
		 
        
        $urlarr['page']	    =	'{{page}}';
        
        $pageurl            =	Url($_GET['m'], $urlarr, 'admin');
        
        $pageM              =	$this  -> MODEL('page');
        
        $pages              =	$pageM -> pageList('crmnew_concern', $where, $pageurl, $_GET['page']);
        
        if ($pages['total'] > 0) {
            
            if ($_GET['order']) {
                
                $where['orderby']   =   $_GET['t'].','.$_GET['order'];
                $urlarr['order']    =   $_GET['order'];
                $urlarr['t']        =   $_GET['t'];
                
            }else{
                
                $where['orderby']   =   'atime';
                
            }
            
            $where['limit']         =   $pages['limit'];
            
            $list                   =   $crmM -> getConcernList($where, array('utype' => 'crm'));
            
            $this -> yunset(array('rows' => $list, 'auid' => intval($_SESSION['auid'])));
            
        }
		
		$power    =  $adminM -> getPower(array('uid'=>$_SESSION['auid']));
		
		$this ->  yunset('power', $power['power']);
        
		$this ->  yuntpl(array('admin/crm_concern'));
    }
	function depart_action(){
		$AdminM			=	$this -> MODEL('admin');
		$CrmM       	=   $this -> MODEL('crm');
		$cacheM 		=   $this -> MODEL('cache');
		$adminUserInfo	=	$AdminM -> getAdminUser(array('uid' => $_SESSION['auid']),array('field'=>'depart'));
		$adminUserList	=	$AdminM -> getList(array('uid'=>array('<>',$_SESSION['auid']),'depart'=>$adminUserInfo['depart']),array('field'=>'`uid`,`name`,`username`'));
		foreach($adminUserList as $v){
			$uids[]		=	$v['uid'];
		}
		$this->yunset('adminUserList',$adminUserList);
		$time	=	intval($_GET['day']);
		if(!empty($time)){
			if($time == 1){ //今天    
			    $startTime   =   mktime(0,0,0,date('m'),date('d'),date('y'));
			    $endTime     =   time();  
			}else if($time == 2){//昨天   
			    $startTime   =   mktime(0, 0, 0, date('m'), date('d'), date('Y'))  - 86400;
			    $endTime     =   mktime(23, 59, 59, date('m'), date('d'), date('Y')) - 86400;
			}else if($time == 3){//本周  
			    $startTime   =	strtotime(date('Y-m-d', strtotime("this week Monday", time())));
			    $endTime     =	strtotime(date('Y-m-d', strtotime("this week Sunday", time()))) + 24 * 3600 - 1;
			}else if($time == 4){//本月			    
			    $startTime   =	mktime(0, 0, 0, date('m'), 1, date('Y'));
			    $endTime     =	mktime(23, 59, 59, date('m'), date('t'), date('Y'));
			}
			$where['PHPYUNBTWSTART_A']   =   '';
			$where['time'][]             =   array('>=', $startTime, 'AND');
			$where['time'][]             =   array('<=', $endTime,'AND');
			$where['PHPYUNBTWEND_A']     =   '';
			$urlarr['day']  =   $time;
		}
		if(!empty($_GET['linktype'])){
			$where['type']		=	$_GET['linktype'];
			$urlarr['linktype']	=	$_GET['linktype'];
		}
		if(!empty($_GET['crmmanager'])) {
			$where['auid']			=	$_GET['crmmanager'];
            $urlarr['crmmanager']	=   $_GET['crmmanager'];
		}else{
			$where['auid']	=	array('in',pylode(',',$uids));
		}
		$urlarr['page']	    =	'{{page}}';    
        $pageurl            =	Url('crm_concern&c=depart', $urlarr, 'admin');    
        $pageM              =	$this  -> MODEL('page');      
        $pages              =	$pageM -> pageList('crmnew_concern', $where, $pageurl, $_GET['page']);  
		if ($pages['total'] > 0) {   
            if ($_GET['order']) { 
                $where['orderby']   =   $_GET['t'].','.$_GET['order'];
                $urlarr['order']    =   $_GET['order'];
                $urlarr['t']        =   $_GET['t'];    
            }else{ 
                $where['orderby']   =   'atime';
            }
            $where['limit']         =   $pages['limit'];
            $list                   =   $CrmM -> getConcernList($where, array('utype' => 'crm')); 
            $this -> yunset('rows',$list);
            
        }
        $cache  =   $cacheM -> GetCache(array('crm'));
        $this -> yunset('cache', $cache);
		$this -> yuntpl(array('admin/crm_concern_depart'));
	}
    
    function show_action()
    {
        $id     =   intval($_POST['id']);
        
        if (!empty($id)) {
            
            $cacheM    =   $this->MODEL('cache');
             
            $cache     =   $cacheM -> GetCache(array('crm'));
             
            
            $crmM   =   $this -> MODEL('crm');
            
            $cInfo  =   $crmM -> getConcernInfo(array('id' => $id));
            
            if ($cInfo) {
                
                $cInfo['time']      =   date('Y-m-d H:i:s', $cInfo['time']);
                $cInfo['atime']     =   date('Y-m-d H:i:s', $cInfo['atime']);
                
                $cInfo['uptime']    =   $cInfo['uptime']? date('Y-m-d H:i:s', $cInfo['uptime']) : '';
                
                $cInfo['type']      =   $cache['crmclass_name'][$cInfo['type']];
                $cInfo['status']    =   $cache['crmclass_name'][$cInfo['status']];
                
                $comM   =   $this -> MODEL('company');
                $com    =   $comM -> getInfo($cInfo['comid'], array('field'=>'`name`,`crm_status`'));
                
                $cInfo['name']      =   $com['name'];
                
                $cInfo['c_status']  =   $cache['crmclass_name'][$com['crm_status']];
                
            }
            
            echo json_encode($cInfo);die;
            
        }
      
    }

	function del_action(){
	
		$this	->	check_token();
		
		$crmM	=	$this -> Model('crm');
		
		$delID	=	$_GET['id'] ? intval($_GET['id']) : $_GET['del'];
		
		$err	=	$crmM -> delConcern($delID);
		
		$this   ->  layer_msg( $err['msg'],$err['errcode'],$err['layertype'],$_SERVER['HTTP_REFERER'],2,1);

	}
}

?>